<?php
/**
 * Created by PhpStorm.
 * User: zouyanan
 * Date: 2019/5/11
 * Time: 下午10:35
 */

namespace app\userapi\controller;
use app\common\controller\Api;


/**
 * 个人中心接口
 */
class Center extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';



    //个人信息
    public function userInfo(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
        $userInfo = $this->auth->getUserinfo();
        $list = db("user a")
          //  ->join("user_renter b","a.id = b.User_id","LEFT")
            ->field("a.mobile,a.nickname,a.avatar,a.num_collect,a.num_attention,a.num_view,a.points,a.signintime")
            ->where("a.id",$userInfo['id'])
            ->find();
        $signintime = $list['signintime'];
        $now = date("Y-m-d",time());
        $signin_time = date("Y-m-d",$signintime);
        if($now == $signin_time){
            $list['signStatus'] = '已签到';
        }else{
            $list['signStatus'] = '签到';
        }
        if(!$list['avatar']){
            $list['avatar'] =  '/assets/img/portrait.png';
        }
        $this->success("返回成功",$list);
    }

    /**
     * 关注列表
     */
    public function attentionList(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
        $userInfo = $this->auth->getUserinfo();
        $limit = $this->request->request('limit',10);
        $list = db("yjy_attention a")
            ->join("user b",'a.agent_id = b.id','LEFT')
            ->field("b.id,b.nickname,b.avatar")
            ->where('a.user_id',$userInfo['id'])
            ->order('a.createtime desc')
            ->paginate($limit);
        $this->success("返回成功",$list);
    }



    /**
     * 收藏列表
     */
    public function collectList(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
        $page = $this->request->request("page");
        //$limit = $this->request->request('limit',10);
        $list = db('yjy_collect a')
            ->join('yjy_house b','a.house_id = b.id','LEFT')
            ->field('b.id,b.image,b.images,b.title,b.price,b.location_near')
            ->where('a.user_id',$userInfo['id'])

            ->order('a.createtime desc')
            ->paginate(6);
        $this->success("返回成功",$list);
    }



    /**
     * 浏览足迹
     */
    public function footprintList(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
       // $limit = $this->request->request('limit',10);
        $page = $this->request->request("page");
        $list = db('yjy_view a')
            ->join('yjy_house b','a.house_id = b.id','LEFT')
            ->field('b.id,b.image,b.images,b.title,b.price,b.location_near')
            ->where('a.user_id',$userInfo['id'])
            ->order('a.createtime desc')
            ->paginate(6);
        $this->success("返回成功",$list);
    }


    //签到
    public function signin(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
        $user = db("user");
        $signintime = $user->where("id",$userInfo['id'])->value("signintime");
        $now = date("Y-m-d",time());
        $signin_time = date("Y-m-d",$signintime);
        if($now == $signin_time){
            $this->error("您今天已经签到了");
        }
        $res = $user->where("id",$userInfo['id'])->update([
            "signintime"=>time()
        ]);
        $user->where("id",$userInfo["id"])->setInc("points",1);
        if($res){
            $this->success("签到成功",$user->where("id",$userInfo["id"])->value("points"));
        }else{
            $this->error("签到失败");
        }
    }



























}